﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;

namespace Server
{
    public class DictionaryService
    {
        private readonly DAL.DictionaryDAL dal = new DAL.DictionaryDAL();

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.DictionaryInfo model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.DictionaryInfo model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.DictionaryInfo GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Model.DictionaryInfo GetModelByCache(int id)
        {

            string CacheKey = "t_dictionaryModel-" + id;
            object objModel = Moon.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Moon.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Moon.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Model.DictionaryInfo)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.DictionaryInfo> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.DictionaryInfo> DataTableToList(DataTable dt)
        {
            List<Model.DictionaryInfo> modelList = new List<Model.DictionaryInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.DictionaryInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.DictionaryInfo();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["Name"] != null && dt.Rows[n]["Name"].ToString() != "")
                    {
                        model.Name = dt.Rows[n]["Name"].ToString();
                    }
                    if (dt.Rows[n]["Values"] != null && dt.Rows[n]["Values"].ToString() != "")
                    {
                        model.Values = dt.Rows[n]["Values"].ToString();
                    }
                    if (dt.Rows[n]["Nodeid"] != null && dt.Rows[n]["Nodeid"].ToString() != "")
                    {
                        model.Nodeid = int.Parse(dt.Rows[n]["Nodeid"].ToString());
                    }
                    if (dt.Rows[n]["ParentID"] != null && dt.Rows[n]["ParentID"].ToString() != "")
                    {
                        model.ParentID = int.Parse(dt.Rows[n]["ParentID"].ToString());
                    }
                    if (dt.Rows[n]["Type"] != null && dt.Rows[n]["Type"].ToString() != "")
                    {
                        model.Type = dt.Rows[n]["Type"].ToString();
                    }
                    if (dt.Rows[n]["ImagePath"] != null && dt.Rows[n]["ImagePath"].ToString() != "")
                    {
                        model.ImagePath = dt.Rows[n]["ImagePath"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string[] orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method


        /// <summary>
        /// 获得所在分类下的Node绑定编号
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>

        public TreeNodeCollection GetAllNode(int NodelId)
        {
            TreeNodeCollection node = new TreeNodeCollection();
            List<Model.DictionaryInfo> list = DataTableToList(dal.GetList(" ParentID=0 and  NodeId=" + NodelId).Tables[0]);
            foreach (Model.DictionaryInfo dr in list)
            {
                TreeNode model = new TreeNode();
                model.Text = dr.Name.Length <= 5 ? dr.Name : dr.Name.Substring(0, 5) + "...";
                model.Value = dr.id.ToString();
             //   CreateTreeViewRecursive(model.ChildNodes, dal.GetList(" ParentID=" + dr.id).Tables[0]);
                node.Add(model);
            }
            return node;
        }


        /// <summary>
        /// 递归获得子节点
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="dataSourse"></param>
        private void CreateTreeViewRecursive(TreeNodeCollection nodes, DataTable dataSourse)
        {

            foreach (DataRow dr in dataSourse.Rows)
            {
                TreeNode node = new TreeNode();
                nodes.Add(node);
                string tmp = dr["Name"].ToString().Length <= 5 ? dr["Name"].ToString() : dr["Name"].ToString().Substring(0, 5) + "...";
                node.Text = tmp;
                node.Value = dr["id"].ToString();
                CreateTreeViewRecursive(node.ChildNodes, dal.GetList(" ParentID=" + dr["Id"]).Tables[0]);
            }
        }
    }
}
